package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entity.Sku;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class GoodsController {

    private final SpuService spuService;

    private final SpuDetailService detailService;

    private final SkuService skuService;

    public GoodsController(SpuService SpuService, SpuDetailService detailService, SkuService skuService) {
        this.spuService = SpuService;
        this.detailService = detailService;
        this.skuService = skuService;
    }

    /**
     * 分页查询spu
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "id", required = false) Long id
    ){
       return ResponseEntity.ok(spuService.querySpuByPage(page, rows, saleable, categoryId, brandId, id));
    }

    /**
     * 新增商品
     * @param spuDTO spu、sku、spuDetail数据集
     * @return 无
     */
    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        // 新增商品
        spuService.saveGoods(spuDTO);
        // 返回201
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改商品
     * @param spuDTO spu、sku、spuDetail数据集
     * @return 无
     */
    @PutMapping
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        // 新增商品
        spuService.updateGoods(spuDTO);
        // 返回201
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 商品的上下架
     * @param id 商品id
     * @param saleable 上架或下架
     * @return 无
     */
    @PutMapping("saleable")
    public ResponseEntity<Void> updateSaleable(
            @RequestParam("id") Long id, @RequestParam("saleable") Boolean saleable){
        // 上下架
        spuService.updateSaleable(id, saleable);
        // 返回204
        return ResponseEntity.noContent().build();
    }

    /**
     * 根据id查询商品
     * @param id 商品id
     * @return spu、sku、spuDetail
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id){
        return ResponseEntity.ok(spuService.queryGoodsById(id));
    }

    /**
     * 根据id查询spuDetail
     * @param id 商品id
     * @return 商品详情
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> queryDetailById(@RequestParam("id") Long id){
        SpuDetail detail = detailService.getById(id);
        return ResponseEntity.ok(new SpuDetailDTO(detail));
    }

    /**
     * 根据spuId查询sku的集合
     * @param id 商品id
     * @return sku的集合
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id") Long id){
        List<Sku> list = skuService.query().eq("spu_id", id).list();
        return ResponseEntity.ok(SkuDTO.convertEntityList(list));
    }

    /**
     * 根据skuId集合查询sku的集合
     * @param ids skuId的集合
     * @return sku的集合
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(SkuDTO.convertEntityList(skuService.listByIds(ids)));
    }

    /**
     * 根据id查询spu，不包含别的
     * @param id 商品id
     * @return spu
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id){
        return ResponseEntity.ok(new SpuDTO(spuService.getById(id)));
    }

    /**
     * 根据商品id和是否参与搜索查询商品的规格参数键值对
     * @param id 商品id
     * @param searching 是否参与搜索
     * @return 参数键值对的集合
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecValues(
            @RequestParam("id") Long id, @RequestParam(value = "searching", required = false) Boolean searching
    ){
        return ResponseEntity.ok(detailService.querySpecValues(id, searching));
    }

    /**
     * 扣减库存
     * @param cartMap 商品集合
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> deductStock(@RequestBody Map<Long, Integer> cartMap){
        skuService.deductStock(cartMap);
        return ResponseEntity.noContent().build();
    }
    /**
     * 批量加库存
     * @param cartMap 商品集合
     */
    @PutMapping("/stock/plus")
    public ResponseEntity<Void> addStock(@RequestBody Map<Long, Integer> cartMap){
        skuService.addStock(cartMap);
        return ResponseEntity.noContent().build();
    }
}